<template>
  <view class="address-wrap">
    <view class="split-black"></view>
    <view class="list">
      <skeleton row="2" :loading="loading">
        <view v-for="item in list" :key="item.id">
          <u-swipe-action
            :btnWidth="btnWidth"
            :show="item.show"
            :key="item.id"
            @click="delCartOne(item)"
            @open="open(item)"
            :options="options"
          >
            <view class="list__li" @tap.stop="handleSelect(item)">
              <view class="list__li--title">
                <view class="list__li--title-name">{{ item.delivery_name }}</view>
                <view class="list__li--title-mobile">{{ item.mobile }}</view>
                <view v-if="item.is_default" class="list__li--title-default">默认</view>
              </view>
              <view class="list__li--address">
                {{ item.region }}{{ item.address }}
              </view>
              <view
              @tap.stop="handleEditAddress(item)"
              :src="addressEditIcon"
              class="iconfont icon-bianji list__li--edit"
               ></view>
            </view>
          </u-swipe-action>
        </view>
      </skeleton>
      <skeleton row="2" :loading="loading"> </skeleton>
    </view>

    <view v-if="!list.length && !loading">
      <u-empty description="暂无收货地址~"></u-empty>
    </view>

    <!-- 操作区 -->
    <view class="add">
      <button class="pp-button add__btn" hover-class="pp-button--active" @tap="handleAddaddress">
        添加收货地址
      </button>
    </view>
  </view>
</template>

<script>
import uEmpty from '@/basic-components/u-empty/index.vue';
import uSwipeAction from '@/basic-components/u-swipe-action/index.vue';
import Skeleton from '@/basic-components/u-skeleton/index.vue';
import { RECEIVER_LIST, DEL_RECEIVER } from '@/api/user';

export default {
  components: {
    uEmpty,
    uSwipeAction,
    Skeleton,
  },
  data() {
    return {
      list: [],
      // 滑动
      btnWidth: 160,
      options: [
        {
          text: '删除',
          style: {
            backgroundColor: '#FF3841',
          },
        },
      ],
      loading: true,
    };
  },
  onShow() {
    console.log("addresss wxx")
    // 列表获取
    this.getReceivers();
  },
  methods: {
    getReceivers() {
      // 地址列表
      this.loading = true;

      this.$http
        .get(RECEIVER_LIST, {
          'per_page': 100
        })
        .then(res => {
          let entry = res.data;
          if (entry && entry.length) {

            // 防止后端类型紊乱，多加一重判断
            entry.length &&
              entry.forEach(item => {
                item.show = false;
              });
            this.list = entry;
          } else {
            this.list = [];
          }

          this.loading = false;
        })
        .catch(() => {
          this.loading = false;
        });
    },
    delCartHandle(id) {
      // 删除地址
      this.$http.delete(`${DEL_RECEIVER}/${id}`).then(() => {
        
        this.getReceivers();
        uni.showToast({
          icon: 'none',
          title: '删除成功',
          duration: 1000,
        });
      });
    },
    async delCartOne(data) {
      // 删除
      const { id } = data;
      const res = await uni.showModal({
        title: '删除地址',
        content: '确认删除该收货地址吗？',
      });

      if (res[1].confirm) {
        this.delCartHandle(id);
      }
    },
    open(data) {
      // 打开
      const { id } = data;

      this.list.forEach(item => {
        if (id === item.id) {
          item.show = true;
        } else {
          item.show = false;
        }
      });
    },
    handleAddaddress() {
      // 添加收货地址
      this.$navigate({
        page: 'addressAdd',
      });
    },
    handleEditAddress(item) {
      // 编辑收货地址
      this.$navigate({
        page: 'addressAdd',
        params: {
          id: item.id,
        },
      });
    },
    handleSelect(item) {
      // 选择地址
      const pages = getCurrentPages();
      const beforePage = pages[pages.length - 2];
      
      // 商品详情——地址选择
      if (
        beforePage && beforePage.route === 'pages/good/confirm-order'
      ) {
        const options = {
          type: 'address',
          params: item,
        };
        uni.navigateBack({
          delta: 1,
          success: () => {
            // 触发上一页更新
            beforePage.onShow(options);
          },
        });
      }
    },
  },
};
</script>

<style lang="less">
.address-wrap {
  position: relative;
  min-height: 100vh;
  padding-bottom: 300rpx;
  padding-bottom: calc(300rpx + constant(safe-area-inset-bottom));
  padding-bottom: calc(300rpx + env(safe-area-inset-bottom));
  background-color: #ffffff;
  box-sizing: content-box;
  .split-black {
    width: 100%;
    height: 20rpx;
    background-color: #f5f5f5;
  }

  .add {
    position: fixed;
    bottom: 0;
    left: 0;
    display: flex;
    justify-content: space-between;
    align-items: center;
    flex-direction: column;
    width: 100%;
    height: 108rpx;
    padding-top: 24rpx;
    padding-bottom: 28rpx;
    padding-bottom: calc(28rpx + constant(safe-area-inset-bottom));
    padding-bottom: calc(28rpx + env(safe-area-inset-bottom));
    box-sizing: content-box;
    &__btn {
      display: flex;
      align-items: center;
      justify-content: center;
      width: 670rpx;
      height: 108rpx;
      font-size: 32rpx;
      font-weight: 500;
      border-radius: 54rpx;
      background-color: #fedd00;
      border: 1px solid #fedd00;
    }

    &__wx {
      display: flex;
      align-items: center;
      justify-content: center;
      width: 670rpx;
      height: 108rpx;
      font-size: 32rpx;
      font-weight: 500;
      border-radius: 54rpx;
      background-color: #03be13;
      color: #ffffff;

      &--img {
        height: 68rpx;
        width: 68rpx;
        margin-right: 14rpx;
      }

      &--text {
      }
    }
  }

  .list {
    padding-left: 40rpx;

    &__li {
      width: 710rpx;
      position: relative;
      padding: 42rpx 30rpx 36rpx 0;
      border-bottom: 1px solid #eeeeee;

      &--title {
        display: flex;
        align-items: center;
        margin-bottom: 8rpx;

        &-name {
          font-size: 30rpx;
          font-weight: 500;
          line-height: 42rpx;
          margin-right: 52rpx;
        }

        &-mobile {
          font-size: 30rpx;
          font-weight: 500;
          line-height: 42rpx;
          margin-right: 18rpx;
        }

        &-default {
          height: 28rpx;
          padding: 0 6rpx;
          border-radius: 6rpx;
          font-size: 20rpx;
          color: #ffffff;
          background-color: #ff3841;
        }
      }

      &--address {
        max-width: 610rpx;
        font-size: 26rpx;
        line-height: 36rpx;
        color: #494949;
      }

      &--edit {
        position: absolute;
        top: 56rpx;
        right: 30rpx;
        width: 60rpx;
        height: 60rpx;
        padding: 20rpx;
      }
    }
  }
}
</style>
